package com.gqz.common.core.domain;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.SchemaProperty;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


@Schema(
        name = "RequestForm",
        description = "统一请求对象（支持feign调用）"
)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RequestForm<T> implements Serializable {
    @SchemaProperty(name = "请求实体")
    private T entity;
    @SchemaProperty(name = "请求T实体的参数集合")
    private Map<String, Object> paramsMap;
    @SchemaProperty(name = "请求页号")
    private @Min(
            value = 1L,
            message = "页索引[pageNo]： 最小不能小于1"
    ) Long pageNo;
    @SchemaProperty(name = "请求页大小")
    private @Min(
            value = 1L,
            message = "页大小[pageSize]： 最小不能小于1"
    ) @Max(
            value = 1000L,
            message = "页大小[pageSize]： 最大不能大于1000"
    ) Long pageSize;
    @SchemaProperty(name = "请求排序字段")
    private String column;
    @SchemaProperty(name = "请求排序类型： desc  asc")
    private String order;

    public static <T> RequestForm<T> newPageInstance(T entity, Long pageNo, Long pageSize, HttpServletRequest req) {
        RequestForm<T> r = new RequestForm();
        r.setEntity(entity);
        r.setPageNo(pageNo == null ? 1L : pageNo);
        r.setPageSize(pageSize == null ? 50L : pageSize);
        r.setParamsMap(getAllRequestParam(req));
        r = formatOrder(r, req);
        return r;
    }

    public static <T> RequestForm<T> newCountInstance(T entity, HttpServletRequest req) {
        RequestForm<T> r = new RequestForm();
        r.setEntity(entity);
        r.setParamsMap(getAllRequestParam(req));
        r = formatOrder(r, req);
        return r;
    }

    private static Map<String, Object> getAllRequestParam(HttpServletRequest request) {
        Map<String, Object> res = new HashMap();
        Enumeration<?> temp = request.getParameterNames();
        if (null != temp) {
            while (true) {
                String en;
                do {
                    String value;
                    do {
                        do {
                            if (!temp.hasMoreElements()) {
                                return res;
                            }

                            en = (String) temp.nextElement();
                            value = request.getParameter(en);
                        } while ("column".equals(en));
                    } while ("order".equals(en));

                    res.put(en, value);
                } while (null != res.get(en) && !"".equals(res.get(en)));

                res.remove(en);
            }
        } else {
            return res;
        }
    }

    private static <T> RequestForm<T> formatOrder(RequestForm<T> r, HttpServletRequest request) {
        String column = request.getParameter("column");
        if (StringUtils.isNotEmpty(column)) {
            r.setColumn(column);
            String order = request.getParameter("order");
            if (StringUtils.isNotEmpty(order)) {
                r.setOrder(order);
            }
        }

        return r;
    }
}
